﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace DataAccess.NHibernateHelper
{
    public abstract class NHibRepository<T> : IRepository<T>, IDisposable
    where T : class
    {
        protected NHibContext Context { get; private set; }
        protected NHibSession Session { get; private set; }

        protected NHibRepository(NHibContext context)
        {
            Context = context;
            Session = Context.CreateNewSession();
        }

        public void Dispose()
        {
            Disposer.TryDispose(Session);
        }

        public void Commit()
        {
            Session.Commit();
        }

        public void Rollback()
        {
            Session.Rollback();
        }

        public virtual void Insert(T item)
        {
            Session.Insert<T>(item);
        }

        public virtual void Update(T item)
        {
            Session.Update<T>(item);
        }

        public virtual void Delete(T item)
        {
            Session.Delete<T>(item);
        }
                
        public virtual IList<T> List()
        {
            return Session.GetList<T>();
        }


        
    }

}
